import { getImgBlob } from "./runcommand";
import { ImageResource } from "./type";

export async function sendImageRequestByUrl(url: string) {
    if (!url.startsWith('http')) {
        //本地图片路径
        url = await getImgBlob(url);
    }
    const body = new URLSearchParams({
        APP: 'getImgUrlMcp',
        VER: '4.0.1.0',
        RULES: '[[]]',
        NOTIFYOPT: '[]',
        CURLOPT: '[]',
        FILTER: '[]',
        EFFECTIVE: 'false',
        PRIORITY: 'HIGH',
        TYPE: '0',
        IMGSTREAM: url.startsWith('http') ? `["${url}"]` : url,
        SIGN: 'c1371596ebf26ce55533358d599c1b36'
    })

    const res = await fetch('http://api.v3.picasso.qhimg.com/v3/app/getImgUrlMcp/tasks/', {
        method: 'POST',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
        body
    });

    if (!res.ok) throw new Error(`sendimg请求失败: ${res.status}`);
    const data = await res.json();
    console.error('sendimg响应数据:', data);
    return data.DATA as string; // 返回groupId
}

export async function fetchUrl(GROUPID: string) {
    try {
        const res = await fetch(`http://api.v3.picasso.qhimg.com/v3/app/getImgUrlMcp/query_tasks/?GROUPID=${GROUPID}`)
        if (!res.ok) throw new Error(`imgurl请求失败: ${res.status}`);
        const data = await res?.json();
        console.error('imgurl响应数据:', data);
        return data?.[0]?.DATA?.[0] as ImageResource; // 返回图片信息对象}
    } catch { }
}
// sendImageRequestByUrl("/Users/zengyufan/Desktop/temp.png")
// console.log(path.resolve());

// getImgUrl('TG_91ea927cd72c0fc03aaf4caf3177da26')
